package com.csu.cphone.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csu.common.utils.PasswdUtil;
import com.csu.cphone.dao.CustomerMapper;
import com.csu.cphone.pojo.CustomerPojo;
import com.csu.cphone.service.CustomerService;

/**
 * @项目名称：cphone
 * @类名称：CustomerService
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年12月5日 下午2:35:43
 * @company:
 */
@Service
public class CustomerServiceImpl implements CustomerService
{
    @Autowired
    private CustomerMapper customerMapper;

    /**
     * @see com.csu.cphone.service.CustomerService#add(java.lang.String,
     *      java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public CustomerPojo add(String number, String wxOpenid, String name, String passwd)
    {
        String passwordMd5 = PasswdUtil.md5Passwd(passwd);
        CustomerPojo customer = new CustomerPojo();
        customer.setName(name);
        customer.setNumber(number);
        customer.setPasswd(passwordMd5);
        customer.setWxOpenid(wxOpenid);
        customerMapper.insert(customer);
        return customer;
    }

    /**
     * @see com.csu.cphone.service.CustomerService#del(java.lang.Integer)
     */
    @Override
    public Integer del(Integer customerId)
    {
        return customerMapper.deleteById(customerId);
    }

    /**
     * @see com.csu.cphone.service.CustomerService#queryById(java.lang.Integer)
     */
    @Override
    public CustomerPojo queryById(Integer customerId)
    {
        return customerMapper.selectById(customerId);
    }

    /**
     * @see com.csu.cphone.service.CustomerService#count()
     */
    @Override
    public Integer count()
    {
        return customerMapper.count();
    }

    /**
     * @see com.csu.cphone.service.CustomerService#queryList(java.lang.Integer,
     *      java.lang.Integer)
     */
    @Override
    public List<CustomerPojo> queryList(Integer pageSize, Integer startIndex, String order)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageSize", pageSize);
        map.put("startIndex", startIndex);
        map.put("order", order);

        return customerMapper.queryList(map);
    }

    /**
     * @see com.csu.cphone.service.CustomerService#update(com.csu.cphone.pojo.CustomerPojo)
     */
    @Override
    public Integer update(CustomerPojo customer)
    {
        String passwd = customer.getPasswd();
        if (passwd != null)
        {
            customer.setPasswd(PasswdUtil.md5Passwd(passwd));
        }

        return customerMapper.update(customer);
    }
}
